<template>
	<view class="container">
		<view class="top">
			
		</view>
		<view class="login-from">
			<form @submit="formSubmit">
			    <!--患者-->
				
			    <!-- <view class="inputView"> 
					 <label class="uni-list-cell-left">患      者:</label>
					 <view class="inputName">
						 <input class="inputText" name="username" bindinput="usernameInput" /> 
					 </view>
					
			    </view>
			
			    <view class="line"></view> -->
				
				 <!--就诊医生-->  
				 <view class="uni-list">
				 	<view class="uni-list-cell">
				 		<view class="uni-list-cell-left">
				 			就诊医生:
				 		</view>
				 		<view class="inputName">
				 			<picker name="doctorId" mode="selector"  @change="bindDoctorChange" :range="doctorList"
							 :range-key="'username'" :value="index">
				 				<view class="uni-input">{{doctorData.username}}</view>
				 			</picker>
				 		</view>
				 	</view>
				 </view>
				 
				<view class="line"></view>
				<view >
					<view class="uni-list-cell-left">症       状:</view>
					<view class="uni-textarea">
						<textarea name="symptoms" class="uni-input" placeholder-style="color:#999999" placeholder="请输入症状"/>
					</view>
				</view>
				<view class="line"></view>	
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							选择日期:
						</view>
						<view class="inputName">
							<picker name="date" mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
								<view class="uni-input" v-if="date">{{date}}</view>
								<view class="uni-input" v-else>请选择</view>
							</picker>
						</view>
					</view>
				</view>
				
				<view class="line"></view>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							选择时间:
						</view>
						<view class="inputName">
							<picker name="time" mode="time" :value="time" start="09:01" end="21:01" @change="bindTimeChange">
								<view class="uni-input">{{time}}</view>
							</picker>
						</view>
					</view>
				</view>
				
				<view class="uni-btn-v">
			        <button form-type="submit">预约</button>
			    </view>
			</form>
		</view>
	</view>
</template>

<script>
	import { getAllDoctorName, addReg } from '@/api/index.js'
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				date: currentDate,
				time: '12:01',
				index: 0,
				doctorList:[],
				doctorData:{
					id: -1,
					username: '请选择'
				}
			}
		},
		computed: {
		        startDate() {
		            return this.getDate('start');
		        },
		        endDate() {
		            return this.getDate('end');
		        }
		},
		onReady() {
			this.getName()
		},
		onLoad() {
			// this.getName()
		},
		
		methods: {
			getName() {
				getAllDoctorName().then(res => {
					if(res.code !== 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.doctorList = res.data.list || []
					console.log(this.doctorList,99)
				})
			},
			bindTextAreaBlur: function (e) {
				console.log(e.detail.value)
			},
			bindDoctorChange: function(e) {
				console.log()
				this.doctorData.id = this.doctorList[e.detail.value].id
				this.doctorData.username = this.doctorList[e.detail.value].username
				
			},
			bindDateChange: function(e) {
				this.date = e.target.value
			},
			bindTimeChange: function(e) {
				this.time = e.target.value
			},
			getDate(type) {
			            const date = new Date();
			            let year = date.getFullYear();
			            let month = date.getMonth() + 1;
			            let day = date.getDate();
			
			            if (type === 'start') {
			                year = year - 60;
			            } else if (type === 'end') {
			                year = year + 2;
			            }
			            month = month > 9 ? month : '0' + month;;
			            day = day > 9 ? day : '0' + day;
			            return `${year}-${month}-${day}`;
			        },
			formSubmit(val) {
				console.log(val)
				let param = {
					patientId: uni.getStorageSync('user').id,
					doctorId: '',
					symptoms: '',
					appointmentTime: ''
				}
				param.doctorId = this.doctorData.id
				param.symptoms = val.detail.value.symptoms
				param.appointmentTime = val.detail.value.date + ' ' + val.detail.value.time+ ':00'
				console.log(param)
				if(!param.doctorId) {
					uni.showToast({
						title: '请选择就诊医生',
						icon: 'none'
					})
				} else if(!param.symptoms) {
					uni.showToast({
						title: '请输入症状',
						icon: 'none'
					})
				} else {
					addReg(param).then(res => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(function(){
							if(res.code == 200) {
								uni.switchTab({
									url: '/pages/index/index'
								})
								return
							}
						}, 1000)
					})
				}
			}
		}
	}
</script>

<style>
	.container {
		height: 100%; 
		 display: flex; 
		 flex-direction: column; 
		 padding: 0; 
		 box-sizing: border-box; 
		/* background: linear-gradient(top, #4a8d9c, #705271); */
	}
	.top {
		padding-top: 40rpx;
		height: 10%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	/*表单内容*/
	.login-from { 
	 flex: auto; 
	 height:100%; 
	 padding: 60rpx;
	 display: flex;
	 align-items: center;
	 justify-content: center;
	}
	.inputView {
	 /* background-color: #fff;  */
	 
	 display: flex;
	 flex-direction: row;
	 align-items: center;

	} 
	.inputName {
		border:1px solid #999999;
		border-radius:10px;
		width: 300rpx;
		height: 50rpx;
		display: flex;
		align-items: center;
	}
	 
	.inputText { 
	 margin-left: 30rpx; 
	 color: #4a8d9c; 
	 font-size: medium;
	 
	} 
	.line { 
	 margin-top: 20rpx; 
	} 
	
	.uni-list-cell {
		display: flex;
		flex-direction: row;
	}
	.uni-list-cell-left {
		padding-right: 20rpx;
		color: #545454;
		font-size: medium;
		white-space: pre-wrap;
	}
	.uni-input {
		color: #4a8d9c;
		font-size: medium;
		padding-left: 20rpx;
	}
	.uni-textarea {
		border:1px solid #999999;
		border-radius:10px;
		margin-top: 10rpx;
		padding: 10rpx;
	}
	button {
	  width: 100%;
	  margin-top: 100rpx;
	  background-color: rgba(74,141,156,0.9);
	  color: white;
	  border-radius: 98rpx;
	  display: flex;
	  flex-direction: row;
	  align-items: center;
	  justify-content: center;
	}
	
	
	button::after {
	  border-radius: 98rpx;
	  border: 0; 
	}
	.uni-btn-v{
		margin-top: 50rpx;
	}
</style>
